//
// Created by Javery Chiang on 2025/5/4.
//

#include "Process.h"


void calculateMetrics(const vector<Process> &process) {
    double total_waiting = 0;
    double total_turnaround = 0;

    cout << "\nPID\tArrival\tBurst\tFinish\tWaiting\tTurnaround\n";

    for (const auto &p: process) {
        cout << p.pid << "\t"
             << p.arrival << "\t" << p.burst << "\t" << p.finish << "\t" << p.waiting << "\t" << p.turnaround << endl;
        total_waiting += p.waiting;
        total_turnaround += p.turnaround;
    }
    // 浮点输出
    cout << fixed << setprecision(2);

    cout << "\nAverage Waiting Time :" << total_waiting / process.size() << endl;

    cout << "\nAverage Turnaround Time :" << total_turnaround / process.size() << endl;

}

void FCFS(vector<Process> process) {
    cout << "\n====First-come first-served ===" << endl;
    sort(process.begin(), process.end(), [](const Process &a, const Process &b) {
        return a.arrival < b.arrival;
    });

    int current_time = 0;

    for (auto &item: process) {
        if (current_time < item.arrival) {
            current_time = item.arrival;
        }
        item.waiting = current_time - item.arrival;

        current_time += item.burst;
        item.finish = current_time;

        item.turnaround = item.finish - item.arrival;
    }
}

void RR(vector<Process> process) {

}

void SRTF(vector<Process> process) {

}